import 'package:base_plugin/base_plugin.dart';
import 'package:finance_app/src/common/app_colors.dart';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter/services.dart';
import 'package:flutter_svg/svg.dart';
import 'package:finance_app/generated/a.dart';

class InputField extends StatefulWidget {
  final Widget leading;
  final String tip;
  final String hint;
  final TextEditingController controller;
  final FocusNode focusNode;
  final String helpText;
  final bool isPassword;
  final bool hasError;
  final TextInputType keyboardType;
  final List<TextInputFormatter> inputFormatters;
  final Widget trailing;
  final EdgeInsets margin;
  final bool enable;
  final bool isBlue;

  const InputField({
    Key key,
    this.helpText,
    this.margin,
    this.leading,
    this.keyboardType,
    this.inputFormatters,
    this.hasError = false,
    this.tip,
    this.hint,
    this.trailing,
    this.controller,
    this.isPassword = false,
    this.focusNode,
    this.enable,
    this.isBlue = false,
  }) : super(key: key);

  @override
  _InputFieldState createState() => _InputFieldState();
}

class _InputFieldState extends State<InputField> {
  bool _hasFocus = false;
  bool obscureText = false;

  @override
  void initState() {
    super.initState();
    widget.focusNode?.addListener(() {
      if (widget.focusNode.hasFocus) {
        if (_hasFocus == false) {
          setState(() {
            _hasFocus = true;
          });
        }
      } else {
        if (_hasFocus == true) {
          setState(() {
            _hasFocus = false;
          });
        }
      }
    });
    if (widget.isPassword) {
      obscureText = true;
    }
  }

  @override
  void dispose() {
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Container(
            margin: widget.margin ??
                EdgeInsets.symmetric(
                  horizontal: getDp(45),
                ),
            height: getDp(54),
            padding: EdgeInsets.symmetric(
              horizontal: getDp(12),
            ),
            decoration: BoxDecoration(
              color: Colors.white,
              border: widget.hasError == true
                  ? Border.all(
                      color: aceRedColor,
                      width: getDp(1),
                    )
                  : (_hasFocus == true
                      ? Border.all(
                          color: aceBlueColor,
                          width: getDp(1),
                        )
                      : Border()),
              boxShadow: [
                BoxShadow(
                  color: Colors.black.withOpacity(0.25),
                  blurRadius: getDp(22),
                  spreadRadius: -getDp(5),
                ),
              ],
              borderRadius: BorderRadius.circular(getDp(15)),
            ),
            child: Row(
              children: [
                if (widget.leading != null) widget.leading,
                SizedBox(
                  width: getDp(8),
                ),
                Expanded(
                    child: TextField(
                  enabled: widget.enable ?? true,
                  inputFormatters: widget.inputFormatters,
                  controller: widget.controller,
                  focusNode: widget.focusNode,
                  scrollPadding: EdgeInsets.zero,
                  obscureText: obscureText,
                  keyboardType: widget.isPassword
                      ? TextInputType.visiblePassword
                      : widget.keyboardType,
                  style: TextStyle(
                    fontSize: getSp(16),
                    color: widget.isBlue ? aceBlueColor : aceBlackColor,
                    fontWeight: FontWeight.bold,
                  ),
                  decoration: InputDecoration(
                      border: InputBorder.none,
                      focusedBorder: InputBorder.none,
                      disabledBorder: InputBorder.none,
                      labelText: widget.tip,
                      hintText: widget.hint,
                      hintStyle: TextStyle(
                          color: widget.hasError ? aceRedColor : aceGrayColor,
                          fontWeight: FontWeight.w700,
                          fontSize: getSp(16),
                          height: 16 / 16),
                      labelStyle: TextStyle(
                          color: widget.hasError ? aceRedColor : aceGrayColor,
                          fontWeight: FontWeight.w700,
                          fontSize: getSp(16),
                          height: 16 / 16)),
                )),
                if (widget.trailing != null) widget.trailing,
                if (widget.isPassword)
                  PressedOpacityButton(
                    onPressed: onChangeObscureText,
                    child: SvgPicture.asset(
                      A.assetsImagesEyes,
                      width: getDp(30),
                      height: getDp(30),
                      fit: BoxFit.contain,
                    ),
                  ),
              ],
            )),
        if (widget.helpText != null)
          Padding(
            padding: EdgeInsets.only(
              left: getDp(59),
              top: getDp(5),
            ),
            child: Text(
              widget.helpText,
              style: TextStyle(
                color: widget.hasError ? aceRedColor : aceDarkGrayColor,
                fontSize: getSp(12),
                fontWeight: FontWeight.bold,
                height: 16 / 12,
              ),
            ),
          ),
      ],
    );
  }

  void onChangeObscureText() {
    setState(() {
      obscureText = !obscureText;
    });
  }
}
